package com.art.base.util;

import com.art.base.R;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * 对话框工具类
 *
 * @author dongtianhao
 * @ClassName: DialogUtil
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2016年7月12日 上午10:49:10
 */
@SuppressLint("NewApi")
public class DialogUtil {

    /**
     * 创建提示对话框
     *
     * @param
     * @return Dialog 返回类型
     * @throws
     * @Title: createAlertDialog
     * @Description: (这里用一句话描述这个方法的作用)
     */
    public static Dialog createAlertDialog(Context context, String title,
                                           int iconId, String msg, DialogInterface.OnClickListener listener1, DialogInterface.OnClickListener listener2) {
        Builder builder = new Builder(context, R.style.AlertDialogTheme);
        builder.setTitle(title);
        builder.setMessage(msg);
        if (iconId != 0) {
            builder.setIcon(iconId);
        }
        builder.setPositiveButton(context.getString(R.string.sure), listener1);
        builder.setNegativeButton(context.getString(R.string.cancel), listener2);
        builder.setCancelable(false);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialog.show();
        return dialog;
    }

    /**
     * 创建提示对话框
     *
     * @param
     * @return Dialog 返回类型
     * @throws
     * @Title: createAlertDialog
     * @Description: (这里用一句话描述这个方法的作用)
     */
    public static Dialog createAlertDialog(Context context, String title,
                                           String msg, DialogInterface.OnClickListener listener1, DialogInterface.OnClickListener listener2) {
        Builder builder = new Builder(context, R.style.AlertDialogTheme);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(context.getString(R.string.sure), listener1);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialog.show();
        return dialog;
    }

    public static Dialog createAlertDialog(Context context, String title,
                                           int iconId, String msg, DialogInterface.OnClickListener listener1) {
        return createAlertDialog(context, title, iconId, msg, listener1, null);
    }

    public static Dialog createAlertDialog(Context context, String title,
                                           String msg, DialogInterface.OnClickListener listener1) {
        return createAlertDialog(context, title, msg, listener1, null);
    }

    /**
     * 单按钮的dialog
     *
     * @param context
     * @param title
     * @param msg
     * @return
     */
    public static Dialog createAlertDialogSingel(Context context, String title,
                                                 String msg, DialogInterface.OnClickListener listener) {
        Builder builder = new Builder(context, R.style.AlertDialogTheme);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(context.getString(R.string.sure), listener);
        builder.setCancelable(false);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialog.show();
        return dialog;
    }

    public static Dialog createAlertDialog(Context context, String title,
                                           String msg) {
        Builder builder = new Builder(context);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(context.getString(R.string.sure), new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setCancelable(false);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialog.show();
        return dialog;
    }

    /***
     * 创建单选对话框
     *
     */
    public static Dialog createSingleSelectionDialog(Context context,
                                                     String title, String[] datas,
                                                     DialogInterface.OnClickListener linister) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        } else {
            builder.setTitle(context.getString(R.string.reminder));
        }
        builder.setItems(datas, linister);
        Dialog dialog = builder.create();
        builder.show();
        return dialog;
    }

    /***
     * 创建复选对话框
     *
     */
    public static Dialog createMultiChoiceDialog(Context context, String title,
                                                 String[] datas, DialogInterface.OnMultiChoiceClickListener linister) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        } else {
            builder.setTitle(context.getString(R.string.reminder));
        }
        builder.setMultiChoiceItems(datas, null, linister);
        Dialog dialog = builder.create();
        builder.show();
        return dialog;
    }

    /**
     * 自定义布局的dialog
     *
     * @param context
     * @param title
     * @param icon
     * @param v        自定义布局
     * @param linister
     * @return
     */
    public static Dialog createViewDialog(Context context, String title,
                                          int icon, View v, boolean showPositiveButton, DialogInterface.OnClickListener positiveLinister,
                                          boolean showNegativeButton, DialogInterface.OnClickListener negativeLinister) {

        Builder builder = new Builder(context, R.style.AlertDialogTheme);
        builder.setTitle(title);
        if (icon != 0) {
            builder.setIcon(icon);
        }
        if (showNegativeButton) {
            builder.setNegativeButton(context.getString(R.string.cancel), negativeLinister);
        }
        if (showPositiveButton) {
            builder.setPositiveButton(context.getString(R.string.sure), positiveLinister);
        }
        builder.setCancelable(false);
        builder.setView(v);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG);
        dialog.show();
        return dialog;

    }

    /**
     * @param context    上下文
     * @param title      标题
     * @param datas      数据来源
     * @param initialize 初选哪些项
     * @param linister   监听
     * @return Dialog 返回类型
     * @throws
     * @Title: createMultiChoiceDialog
     * @Description: 创建复选对话框
     */
    public static Dialog createMultiChoiceDialog(Context context, String title,
                                                 String[] datas, boolean[] initialize,
                                                 DialogInterface.OnMultiChoiceClickListener linister) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        } else {
            builder.setTitle(context.getString(R.string.reminder));
        }
        builder.setMultiChoiceItems(datas, initialize, linister);
        Dialog dialog = builder.create();
        builder.show();
        return dialog;
    }

    /**
     * @param @param  context 上下文
     * @param @param  msg
     * @param @return
     * @return ProgressDialog 返回类型
     * @throws
     * @Title: createLoadingDialog
     * @Description:创建进度条对话框
     */
    public static ProgressDialog createLoadingDialog(Context context, String msg) {
        ProgressDialog dialog = new ProgressDialog(context, R.style.AlertDialogTheme);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        if (TextUtils.isEmpty(msg)) {
            dialog.setMessage(context.getString(R.string.isloading));
        } else {
            dialog.setMessage(msg);
        }
        dialog.show();
        return dialog;

    }
}
